<template>
  <div>
    <el-form :model="pageForm" inline>
      <el-form-item label="仓库：">
        <el-select v-model="pageForm.warehouseId">
          <el-option label="全部" :value="-1" />
          <el-option :label="item.name" :value="item.id" v-for="item in store.NewHouseList" :key="item.id" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="订单号：">
        <el-input v-model="form.name" />
      </el-form-item> -->
      <el-form-item label="订单来源平台：">
        <el-select v-model="pageForm.dataFrom">
          <el-option :label="item" :value="item" v-for="(item, index) in shujuSelectList" :key="index" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属公司：">
        <el-select v-model="pageForm.belongCompany">
          <el-option :label="item" :value="item" v-for="(item, index) in gongsiHouseList" :key="index" />
        </el-select>
      </el-form-item>
      <el-form-item label="销毁状态：">
        <el-select v-model="pageForm.region">
          <el-option label="全部" :value="-1" />
          <el-option label="待销毁" :value="0" />
          <el-option label="已销毁" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="确认时间:">
        <el-date-picker v-model="times" type="datetimerange" start-placeholder="开始时间" value-format="x"
          end-placeholder="结束时间" time-format="A hh:mm:ss" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">搜索</el-button>
        <el-button type="primary" @click="onSubmit">生成销毁单</el-button>
      </el-form-item>
    </el-form>
    <div class="table">
      <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column label="销毁编号" width="120">
          <template #default="scope">{{ scope.row.destructionNum }}</template>
        </el-table-column>
        <el-table-column label="仓库" width="250">
          <template #default="scope">{{ scope.row.warehouseName }}</template>
        </el-table-column>
        <el-table-column label="所属公司" width="120">
          <template #default="scope">{{ scope.row.belongCompany }}</template>
        </el-table-column>
        <el-table-column label="提交时间" width="180">
          <template #default="scope">{{ scope.row.createTime }}</template>
        </el-table-column>
        <el-table-column label="提交用户" width="220">
          <template #default="scope">{{ scope.row.createBy }}</template>
        </el-table-column>
        <el-table-column label="商品信息" width="150">
          <template #default="scope">{{ scope.row.sku }}</template>
        </el-table-column>
        <el-table-column label="所在库位" width="150">
          <template #default="scope">{{ scope.row.warehouseLocationName }}</template>
        </el-table-column>
        <el-table-column label="销毁状态" width="150">
          <template #default="scope">{{ scope.row.isDestruction == 0 ? '未销毁' : '已销毁' }}</template>
        </el-table-column>
        <el-table-column label="销毁时间" width="150">
          <template #default="scope">{{ scope.row.destructionTime }}</template>
        </el-table-column>
        <el-table-column label="数据来源" width="150">
          <template #default="scope">{{ scope.row.dataFrom }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button link type="primary" size="small"
              @click.prevent="afterDestructionOrderdestructionBtn(scope.$index)">
              销毁
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <div class="page">
    <el-pagination v-model:current-page="currentPage4" v-model:page-size="pageSize4" :page-sizes="[10, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />
  </div>
</template>

<script setup>
import { afterDestructionOrdergetBelongCompanyList, afterDestructionOrderList, afterDestructionOrdergetDataFromList, afterDestructionOrderdestruction } from '@/api/index'
import { ref, onBeforeMount } from 'vue'
import { ElMessage } from 'element-plus'
import { useAlertsStore } from '@/store/index'
const store = useAlertsStore()
const pageForm = ref({
  warehouseId: -1,
  belongCompany: '全部',
  beginDateTime: '',
  endDateTime: '',
  isDestruction: -1,
  dataFrom: '全部',
  pageSize: 100,
  pageNum: 1
})
// const form = ref({})
onBeforeMount(() => {
  gongsiHouse()
  shujuSelect()
  store.wareHouseSelect()
})
const onSubmit = () => {
  getList()
}
// table多选
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}
const getList = () => {
  if (times.value) {
    pageForm.value.beginDateTime = times.value[0]
    pageForm.value.endDateTime = times.value[1]
  } else {
    pageForm.value.beginDateTime = ''
    pageForm.value.endDateTime = ''
  }
  afterDestructionOrderList(pageForm.value).then(res => {
    const { data: { data } } = res
    total.value = data.total
    tableData.value = data.list
  })
}
const gongsiHouseList = ref([])
const gongsiHouse = () => {
  afterDestructionOrdergetBelongCompanyList().then(res => {
    console.log(res)
    const { data: { data } } = res
    gongsiHouseList.value = data
  })
}
// 下拉数据
const shujuSelectList = ref([])
const shujuSelect = () => {
  afterDestructionOrdergetDataFromList().then(res => {
    console.log('res')
    console.log(res)
    const { data: { data } } = res
    shujuSelectList.value = data
  })
}
// 分页
const tableData = ref([])
const currentPage4 = ref(1)
const pageSize4 = ref(10)
const total = ref(0)
const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
  pageForm.value.pageSize = val
  getList()
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  pageForm.value.pageNum = val
  getList()
}
const times = ref([])
const afterDestructionOrderdestructionBtn = (row) => {
  afterDestructionOrderdestruction({ id: row.id }).then(() => {
    ElMessage.success('操作成功')
    getList()
  })
}
</script>

<style lang='less' scoped></style>
